﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RobotController
{
    public partial class MainController : Form
    {
        DataTable pokerList = null;

        public MainController()
        {
            InitializeComponent();

            InitForm();
        }

        private void MainController_Load(object sender, EventArgs e)
        {

        }


        #region "Load data"

        private bool InitForm()
        {
            try
            {
                DataSet ds = new DataSet();
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                var dataPath = System.IO.Path.Combine(Application.StartupPath, @"PokerListData.xml");

                doc.Load(dataPath);

                var reader = new System.Xml.XmlNodeReader(doc);
                ds.ReadXml(reader);

                pokerList = ds.Tables[0];
                AddMoreColumns();

                grdInfo.DataSource = pokerList.DefaultView;

                reader = null;
                ds = null;
                doc = null;

                formatGrid();

                foreach (DataGridViewColumn iCol in grdInfo.Columns)
                {
                    if (iCol.Name == "Select") continue;
                    iCol.ReadOnly = true;
                }

                grdInfo.Focus();

                return true;
            }
            catch { return false; }
        }

        private void AddMoreColumns()
        {
            if (pokerList == null) return;

            DataColumn col = new DataColumn();
            col.ColumnName = "Select";
            col.DataType = typeof(System.Boolean);
            pokerList.Columns.Add(col);
            col.SetOrdinal(1);

            col = new DataColumn();
            col.ColumnName = "Status";
            col.DataType = typeof(System.String);
            pokerList.Columns.Add(col);

            col = new DataColumn();
            col.ColumnName = "StartDate";
            col.DataType = typeof(System.DateTime);
            pokerList.Columns.Add(col);

            col = new DataColumn();
            col.ColumnName = "ElapseTime";
            col.DataType = typeof(System.DateTime);
            pokerList.Columns.Add(col);

            col = pokerList.Columns["RobotPath"];
            col.SetOrdinal(pokerList.Columns.Count - 1);

            return;
        }

        private void formatGrid()
        {
            grdInfo.Columns["No"].Width = 40;
            grdInfo.Columns["Select"].Width = 50;
            grdInfo.Columns["Status"].Width = 150;
            grdInfo.Columns["Status"].HeaderText = "Current Status";
            grdInfo.Columns["Name"].Width = 120;
            grdInfo.Columns["Email"].Width = 150;
            grdInfo.Columns["Password"].Width = 120;
            grdInfo.Columns["RobotPath"].Width = 300;            
            grdInfo.Columns["RobotPath"].HeaderText = "Poker path";

            grdInfo.Columns["StartDate"].HeaderText = "Start Date Time";
            grdInfo.Columns["StartDate"].Width = 120;
            grdInfo.Columns["StartDate"].DefaultCellStyle.Format = "dd/MM/yyyy hh:MMM:ss";
            grdInfo.Columns["StartDate"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            grdInfo.Columns["ElapseTime"].HeaderText = "Elapsed Time";
            grdInfo.Columns["ElapseTime"].DefaultCellStyle.Format = "hh:MMM:ss";            
            grdInfo.Columns["ElapseTime"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }

        #endregion

        private void grdInfo_ColumnHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex != 1) return;
            if (pokerList == null || pokerList.Rows.Count < 1) return;

            var selectedAll = false;
            if (pokerList.Rows.Cast<DataRow>().Any(r => r["Select"] is DBNull || r["Select"].ToString() == bool.FalseString))
                selectedAll = true;
            else
                selectedAll = false;
            foreach (DataRow item in pokerList.Rows)
            {
                item["Select"] = selectedAll;
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (grdInfo.CurrentRow == null)
            {
                MessageBox.Show("Please select at least a poker to play", "Poker main controller", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
        }

    }
}
